<?

/* Login
*****************************************************************************/

// Load classes
include_once('includes/loader.inc.php');
$user = new User();

// If the user wants to log out by refering /magnet/logout/
if ($logout) {
	$user->logout();
	header("Location: /magnet/login/");
	exit;
}

// If the user is logged in, redirect
if ($user->logged()) {
	header("Location: /magnet/admin/"); // todo . Change "admin" for the user's acl route
	exit;
}

// Login attempt
if (!empty($_POST)) {
	$username = addslashes($_POST['username']);
	$password = addslashes($_POST['password']);
	if (empty($username) or empty($password)) {
		$error = 'Please complete all the fields';
	} else {
		if(!$log = $user->login($username, $password)) {
			$error = 'Error: ' . $user->error();
		} else {
			header("Location: " . $_SERVER["HTTP_REFERER"]);
			exit;
		}
	}
}

include('themes/' . CONFIG_THEME . '/login.tpl');

?>